package com.iptv.grape.bu.process;

import android.os.Handler;
import android.text.TextUtils;

import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.BillAppointment;
import com.iptv.grape.domain.BillAppointmentContainer;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;

import java.util.List;

public class BillAppointmentProc {

    public static final int HandlerTimerVal = 10000;


    public BillAppointmentProc() {

    }

    Handler taskHandler = new Handler();

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try{
                if(GrapeContext.curGrapeContext().isShowAppUI()){
                    runTask();
                }

            }
            catch (Exception e){
                e.printStackTrace();
                AppLog.getCurLog().out(e.getMessage());
            }
            taskHandler.postDelayed(this, HandlerTimerVal);
        }
    };
    public void init(){
        taskHandler.postDelayed(runnable, HandlerTimerVal);
    }

    private void runTask(){
        //GrapeContext.curGrapeContext().getBillAppointmentContainer().initData();
        List<BillAppointment> billAppointments = GrapeContext.curGrapeContext().getBillAppointmentContainer().getBillAppointmentList();
        for(int i = 0; i < billAppointments.size(); i++){
            BillAppointment billAppointment = billAppointments.get(i);
            if(!billAppointment.isCancel() && !billAppointment.isEmpty()){

                //billAppointment.getChannelBill().getBillStartDate().get
                boolean isInTime = CommUtil.equalsBillDate(billAppointment.getChannelBill().getBillStartDate());
                if(isInTime){
                    boolean isShow = false;
                    if(null==GrapeContext.curGrapeContext().getChannelDLinkList().getCurChannel()){
                        isShow = true;
                    }else{
                        String  keyId = GrapeContext.curGrapeContext().getChannelDLinkList().getCurChannel().getKeyID();
                        AppLog.getCurLog().e("showKeyID = ",keyId,"==>ChannelKeyID = ",billAppointment.getChannelKeyID());
                        if(TextUtils.isEmpty(keyId)|| !keyId.equals(billAppointment.getChannelKeyID())){
                            isShow = true;
                        }
                    }
                         // 判断当前是否正在观看这个频道 否则弹出提醒对话框
                    if(isShow){
                        // 弹出提醒对话框
                        AppLog.getCurLog().e("=====================================>>>>>   弹出对话框");
                        GrapeContext.curGrapeContext().getCurActivity().showAppointmentReminder(billAppointment.getChannel(),
                                billAppointment.getChannelBill());
                        billAppointment.setCancel(true);
                    }

                }
            }
        }

        //AppLog.getCurLog().out("runTask-------------");
    }

}
